﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ext.Net;
namespace Enterprise.Web.Controls
{
    public class XLookup: ComboBox
    {
        private Store store;
        private JsonReader reader;
        public XLookup()
        {
            this.BuildStore();
            this.BuildTrigger();
            this.BuildListener();
            this.DisplayField = "CodeValue";
            this.ValueField = "CodeID";
        }

        private void BuildListener()
        {
            this.Listeners.Select.Handler = "this.triggers[0].show();";
            this.Listeners.BeforeQuery.Handler = "this.triggers[0][ this.getRawValue().toString().length == 0 ? 'hide' : 'show']();";
            this.Listeners.TriggerClick.Handler = "if (index == 0) { this.clearValue(); this.triggers[0].hide(); }";
        }

        private void BuildTrigger()
        {
            FieldTrigger trigger = new FieldTrigger();
            trigger.Icon = Ext.Net.TriggerIcon.Clear;
            trigger.HideTrigger = true;
            this.Triggers.Add(trigger);
        }

        private void BuildStore()
        {
            this.store = new Store();
            this.reader = new Ext.Net.JsonReader();
            this.reader.IDProperty = "CodeID";
            this.reader.Fields.Add("CodeID", RecordFieldType.Int);
            this.reader.Fields.Add("CodeValue", RecordFieldType.String);
            this.store.Reader.Add(reader);
            this.Bin.Add(this.store);
        }

        public void SetDataSource(IEnumerable<object> datasource)
        {
            this.store.DataSource = datasource;
            this.store.DataBind();
        }

        public override void DataBind()
        {
            if (!string.IsNullOrEmpty(this.TypeName) && !string.IsNullOrEmpty(this.SelectMethod))
            {
                this.SetDataSource(InvokeStringMethod(this.TypeName, this.SelectMethod));
            }
        }

        public override void SetValue(object value)
        {
            if ((value!=null) && (value.ToString().Equals("0")))
            {
                value = null;
            }

            base.SetValue(value);
        }

        #region override method
        protected override void OnBeforeClientInit(Observable sender)
        {
            this.StoreID = this.store.ID;
            if (this.AutoLoad && !string.IsNullOrEmpty(this.TypeName) && !string.IsNullOrEmpty(this.SelectMethod))
            {
               this.SetDataSource(InvokeStringMethod(this.TypeName, this.SelectMethod));
            }
            base.OnBeforeClientInit(sender);
        }



        #endregion

        public string SelectMethod { get; set; }
        public string TypeName { get; set; }
        public bool AutoLoad { get; set; }

        public IEnumerable<object> InvokeStringMethod(string typename, string methodName)
        {
            Type calledType = Type.GetType(typename);
            object data = calledType.InvokeMember(methodName, System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static, null, null, null);
            return (IEnumerable<object>)data;
        }

      
    }
}

